import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../provider/_providers.dart';
import '../../model/_models_db.dart';


class ContactListUI extends ConsumerWidget {
  ContactListUI({super.key});


  List<Map> items = [
    {"id": "01", "name": "张三", "icon": Icons.add},
    {"id": "02", "name": "李四", "icon": Icons.person},
    {"id": "03", "name": "王五", "icon": Icons.new_label}
  ];

  @override
  Widget build(BuildContext context, WidgetRef ref) {return Consumer(
      builder: (context, ref, child)
  {
      var addressbooks =  ref.watch(contactProvider).addressbooks;

      return buildList(context, addressbooks,ref);
  });
  }

  Widget buildList(BuildContext context, List<ContactModel> books,WidgetRef ref) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 35,
        backgroundColor: Colors.white,
        shadowColor: Colors.transparent,
        title: const Text(
          '常用联系人',
          style: TextStyle(color: Colors.grey, fontSize: 14),
        ),
        actions: [],
      ),
      // ignore: avoid_unnecessary_containers
      body: Container(
        child: ListView.separated(
          // To add separation line between the ListView
          separatorBuilder: (context, index) =>
              Divider(color: Colors.transparent),

          itemCount: books.length,
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
              hoverColor: Colors.lightBlue,
              onTap: () {
                print("chat me");

                ref.read(chatManagerProvider).startConversation(books[index]);

              },
              title: Text(books[index].Friendname),
              trailing: books[index].Online
                  ? Text(
                      "在线",
                      style: TextStyle(color: Colors.green),
                    )
                  : Text("离线"),
            );
          },
        ),
      ),
    );
  }
}
